﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.IO;
using System.Web.UI;
using Microsoft.Security.Application;

namespace InhollandAirPortal {
  public class Reis {
    #region parameters
    public int ID { get; private set;}
    public string code { get; private set; }
    public int prijs { get; private set; }
    public string naam { get; private set; }
    public string omschrijving { get; private set; }
    public DateTime datum { get; private set; }
    #endregion

    public Reis(){
    ID = -1;
    code = String.Empty;
    prijs = -1;
    naam = String.Empty;
    omschrijving = String.Empty;
    datum = DateTime.MinValue;
    }

    public Reis(int AReisID):this() {
      Load(AReisID);
    }

    public Reis(string ACode):this(){ // Reis ophalen uit database
      using (LoyalityDB DB = new LoyalityDB()) {
        IDataReader Reader = DB.Reis_GetReisByCode(ACode);
        if (Reader.Read()) {
          Load(Reader);
        }
      }
    }

    public Reis(IDataRecord ARecord) {
      Load(ARecord);
    }

		private void Load(int aReisID) { // Reis ophalen uit database
      using (LoyalityDB DB = new LoyalityDB()) {
        IDataReader Reader = DB.Reis_GetReisByID(aReisID);
        if (Reader.Read()) {
          Load(Reader);
        }
      }
    }

    private void Load(IDataRecord ARecord) {
      ID = LoyalityDB.GetInt32Value(ARecord, "id", -1);
      code = Encoder.HtmlEncode(LoyalityDB.GetStringValue(ARecord, "code", String.Empty));
      prijs = LoyalityDB.GetInt32Value(ARecord, "prijs", -1);
      naam = CustomSanitize.CleanAndEncode(CustomSanitize.regex_LettersCijfers, LoyalityDB.GetStringValue(ARecord, "naam", String.Empty));
      omschrijving = CustomSanitize.CleanAndEncode(CustomSanitize.regex_LettersCijfersExtra, LoyalityDB.GetStringValue(ARecord, "omschrijving", String.Empty));
      datum = LoyalityDB.GetDateTimeValue(ARecord, "datum", DateTime.MinValue);
    }

    public static string BouwReisDiv(Reis AReis, bool MetBevestigKnop) { // Div opbouwen om reis te weergeven
         StringWriter stringWriter = new StringWriter();
					using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter)) {
          writer.AddAttribute(HtmlTextWriterAttribute.Class, "reis");
          writer.RenderBeginTag(HtmlTextWriterTag.Div);
					// Plaatje weergeven bij reis
          writer.AddAttribute(HtmlTextWriterAttribute.Src, VirtualPathUtility.ToAbsolute("~/img/reizen/") + Encoder.HtmlEncode(AReis.code) + ".jpg");
          writer.AddAttribute(HtmlTextWriterAttribute.Class, "reisPlaatje");
          writer.RenderBeginTag(HtmlTextWriterTag.Img);
          writer.RenderEndTag();
					// Naam toevoegen
          writer.AddAttribute(HtmlTextWriterAttribute.Class, "reisnaam");
          writer.RenderBeginTag(HtmlTextWriterTag.H3);
          writer.Write(HttpUtility.HtmlEncode(AReis.naam));
          writer.RenderEndTag();
					// Datum toevoegen
          writer.AddAttribute(HtmlTextWriterAttribute.Class, "reisdatum");
          writer.RenderBeginTag(HtmlTextWriterTag.Div);
          writer.Write("Datum: " + Encoder.HtmlEncode(AReis.datum.ToShortDateString()));
          writer.RenderEndTag();
					// Kosten in miles
          writer.AddAttribute(HtmlTextWriterAttribute.Class, "reisPunten");
          writer.RenderBeginTag(HtmlTextWriterTag.Div);
          writer.Write("&curren;" + AReis.prijs.ToString());
          writer.RenderEndTag();
					// Omschrijving
          writer.AddAttribute(HtmlTextWriterAttribute.Class, "reisOmschrijving");
          writer.RenderBeginTag(HtmlTextWriterTag.Div);
          writer.Write(HttpUtility.HtmlEncode(AReis.omschrijving));
          writer.RenderEndTag();
          if (MetBevestigKnop) {
            // Knop om reis te kopen
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "btnKoopReis");
            writer.AddAttribute(HtmlTextWriterAttribute.Href, VirtualPathUtility.ToAbsolute("~/KoopReis.aspx") + "?RC=" + Encoder.HtmlEncode(AReis.code));
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write("Bestellen &raquo;");
            writer.RenderEndTag();
          }
          writer.RenderEndTag();// end of reis
          return writer.InnerWriter.ToString();
        }      
    }
  }

  public class ReisDatums : List<DateTime> { // Haal een lijst op met datums waarop reizen beschikbaar zijn
    public ReisDatums() {
      using (LoyalityDB DB = new LoyalityDB()) {
        IDataReader Reader = DB.Reis_GetReisDatums();
        while (Reader.Read()) {
          DateTime Datum = LoyalityDB.GetDateTimeValue(Reader, "datum", DateTime.MinValue);
          Add(Datum);
        }
      }
    }
  }

  public class ReisLijst : List<Reis> {
    public ReisLijst(){
    }
    public ReisLijst(DateTime ADatum):this() { // Haal reizen op voor deze datum
      using (LoyalityDB DB = new LoyalityDB()) {
        IDataReader Reader = DB.Reis_GetReisLijstByDatum(ADatum);
        while (Reader.Read()) {
          Add(new Reis(Reader));
        }
      }
    }
  }

}